$(document).ready(function () {
    var page = 1;

    function addModalListener() {
        $(".order-info").each(function () {
            var self = this;
            $(this).click(function () {
                var dataElement = $(self).children()[0];
                $("#o_status").html($(dataElement).attr("orderstatus"));
                $("#start_time").html($(dataElement).attr("starttime"));
                $("#end_time").html($(dataElement).attr("endtime"));
                $("#driver").html($(dataElement).attr("driver"));
                $("#driver_tel").html($(dataElement).attr("drivertel"));
                $("#car_info").html($(dataElement).attr("carinfo"));
                $("#orderDetail").modal();
            });
        });
    };
    addModalListener();

    if($("#userinfo").attr("sex")) {
        $("input[value="+ $("#userinfo").attr("sex") +"]").attr('checked',$("#userinfo").attr("sex"));
        $()
    }

    $("#avatarUpload").click(function(){
        $("#avatarInput").click();
    });

    $("#avatarInput").on('change',function(){
        $("form").submit();
    });

    $("#up").click(function () {
        if (page > 1) {
            --page;
            findOrderList();
        }
    });
    $("#down").click(function () {
        ++page;
        findOrderList();
    });

    $("#saveModify").click(function(){
        var data = {};
        data.sex = $("#sex").val();
        data.nick_name = $("#nick_name").val();
        data.real_name = $("#real_name").val();
        data.information = {};
        data.information.weixin = $("#weixin").val();
        data.information.alipay = $("#alipay").val();
        data.information.qq = $("#qq").val();
        $.post("/user/modify",data,function(data) {
            console.log(data);
        })
    });

    function findOrderList() {
        $.post('/user/order', {page: page}, function (data) {
            console.log(data);
            if (data.length < 1) {
                window.utils.toast('warning', "已翻至最后一页");
            } else {
                var html = "";
                data.forEach(function (e) {
                    html += '<div class="order-info">\
                <p style="display: none" orderstatus="' + e.status + '" starttime="' + e.start_time + '" \
                endtime="' + e.end_time + '" driver="' + e.driver_info.name + '" drivertel="' + e.driver_info.tel + '" \
                reqcarinfo="' + e.car_info.req_car_type + '" \
                carinfo="' + e.car_info.car_type + ' ' + e.car_info.car_number + '"></p>\
                <p class="col-xs-12 col-sm-12 margin-less">订单号：<span style="color: #F33">' + e.createdAt + '</span>\
                <span style="float: right;border:1px solid #aaa;border-radius: 3px;padding:1px 5px 1px 5px;color:#555;font-size: 10px">' + e.order_type + '</span>\
                </p>\
                <p class="col-xs-12 col-sm-12 margin-less">出发地：<span style="color: #F33">' + e.start_addr.name + '</span></p>\
                <p class="col-xs-12 col-sm-12 margin-less" style="margin-top: 1px">目的地：<span style="color: #F33">' + e.start_addr.name + '</span></p>\
                </div>\
                <div class="col-xs-12 col-md-12 h-line-grey" style="border-color:#bbb;margin-bottom: 5px;"></div>';
                });
                if (data.length < 4) {
                    html += '<div style="text-align: center;margin-top: 5px" class="col-xs-12 col-sm-12">暂无更多记录</div>';
                }
                $('#orderListContainer').html(html);
                addModalListener();
            }
        });
    }
});